//
//  LoginViewController.m
//  BMProject
//
//  Created by 段大志 on 15/12/27.
//  Copyright © 2015年 MengHuan. All rights reserved.
//

#import "LoginViewController.h"
#import "JSONModel.h"
#import "UserModel.h"
#import "UIColor+Extension.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *loginTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"--LoginViewController--");
    self.passwordTF.secureTextEntry = YES;
    self.loginBtn.layer.cornerRadius = 5;
    self.loginBtn.layer.borderWidth = 1;
    self.loginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.loginBtn.layer.backgroundColor = [UIColor hexValue:0xa5ddef].CGColor;
    // Do any additional setup after loading the view from its nib.
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)loginAction:(id)sender {
    if (self.loginTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入用户名"];
        return;
    }
    if (self.passwordTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    [self.view endEditing:YES];
    [UserServices loginByUsername:self.loginTF.text
                             pass:self.passwordTF.text
                  completionBlock:^(int result, id responseObject) {
                      if (result == API_Request_Success) {
                          NSError *error;
                          responseObject = [NSJSONSerialization JSONObjectWithData:responseObject options:nil error:&error];
                          if (responseObject  && [responseObject isKindOfClass:[NSArray class]]) {
                              NSDictionary *dictionary = responseObject[0];
                              if ([dictionary objectForKey:@"msg"]) {
                                  [SVProgressHUD showErrorWithStatus:[dictionary objectForKey:@"msg"]];
                                  return;
                              } else {
                                  [SVProgressHUD showSuccessWithStatus:@"登录成功!"];
                                  UserModel *userModel = nil;
                                  userModel =  [[UserModel alloc] initWithDictionary:dictionary error:nil];
                                  userModel.username = self.loginTF.text;
                                  userModel.pwd = self.passwordTF.text;
                                  userModel.griddingArea  =  dictionary[@"griddingArea"];
                                  [[AppDelegate appDelegate] setG_UserInfo:userModel];

                                  [[AppDelegate appDelegate] setRootVC];
                                  return;
                              }
                          }
                      }
                  }];
}

@end
